<%@ include file="inc/taglibs.jsp" %>
<%@tag import="java.io.IOException"%>
<%@ tag import="com.pearlsoft.util.StringUtils" %>
<%@tag import="com.pearlsoft.ui.core.TagUtils"%>
<%@ tag import="com.pearlsoft.ui.core.PageContextUtils" %>
<%@ attribute rtexprvalue="true" required="false" name="scripts" description="在输出script之前要输出的script url，多个url用逗号隔开，如是相对路径（不以http://开头）则是相对于项目根目录"%>
<%@ attribute rtexprvalue="true" required="false" name="styles" description="在输出script之前要输出的css url，多个url用逗号隔开，如是相对路径（不以http://开头）则是相对于项目根目录"%>
<%@ attribute rtexprvalue="true" required="false" name="lazyScripts" description="在输出script之后要输出的script url，多个url用逗号隔开，如是相对路径（不以http://开头）则是相对于项目根目录"%>
<c:choose>
	<c:when test="<%=!initResources()%>">
		<jsp:doBody />
	</c:when>
	<c:otherwise>
		<jsp:doBody />
		<c:set var="bufferedScripts" value="<%=getBufferedScripts()%>"></c:set>
		<c:choose>
			<c:when test="${empty(bufferedScripts)}">
<script type="text/javascript">
Ext.onReady(function(){
	<%printBodyBuffer();%>
});
</script>
			</c:when>
			<c:otherwise>
<script type="text/javascript">
Ext.onReady(function(){
	LazyLoad.js("${bufferedScripts}",function(){
		<%printBodyBuffer();%>
	});
});
</script>
			</c:otherwise>
		</c:choose>
	</c:otherwise>
</c:choose>
<%!

private boolean initResources() throws JspException{
	JspContext c=this.getJspContext();
	boolean result=PageContextUtils.initPageBuffer(c);
	PageContextUtils.addResources(c,scripts,styles,lazyScripts);
	return result;
}

private void printBodyBuffer() throws JspException{
	try{
		PageContextUtils.printBuffer(this.getJspContext(),PageContextUtils.TOP_SCRIPT_BUFFER);
		PageContextUtils.printAllJSComponents((PageContext)this.getJspContext());
		PageContextUtils.printBuffer(this.getJspContext(),PageContextUtils.BOTTOM_SCRIPT_BUFFER);
		PageContextUtils.printStyles((PageContext)this.getJspContext(),PageContextUtils.STYLES_BUFFER);
		PageContextUtils.printLazyScripts((PageContext)this.getJspContext(),PageContextUtils.LAZY_SCRIPTS_BUFFER);
	}
	catch(IOException e){
		throw new JspException(e);
	}
}

private String getBufferedScripts() throws JspException {
	try {
		String scripts = PageContextUtils.getBufferedScripts((PageContext)this.getJspContext(),PageContextUtils.CMP_SCRIPTS_BUFFER);
		((PageContext)this.getJspContext()).removeAttribute(PageContextUtils.CMP_SCRIPTS_BUFFER, PageContext.REQUEST_SCOPE);
		return scripts;
	}
	catch(Exception e) {
		throw new JspException(e);
	}
}

%>